package com.leslie97.app.nightlight;

import java.util.List;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.*;

public class NightLight extends Activity implements OnClickListener {
    private static String TAG = "NightLight";

    private static int TURN_OFF_AFTER_1MINUTE = 60000;
    private static int TURN_OFF_AFTER_15MINUTES = 15*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_20MINUTES = 20*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_30MINUTES = 30*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_45MINUTES = 45*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_60MINUTES = 60*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_90MINUTES = 90*TURN_OFF_AFTER_1MINUTE;
    private static int TURN_OFF_AFTER_120MINUTES = 120*TURN_OFF_AFTER_1MINUTE;

    private Button mFifteenMinutes;
    private Button mTwentyMinutes;
    private Button mThirtyMinutes;
    private Button mFortyFiveMinutes;
    private Button mSixtyMinutes;
    private Button mNinetyMinutes;
    private Button mTwoHours;
    private Button mExit;

    private Camera mCamera = null;
    private Parameters mParameters = null;
    private boolean mCameraPreviewing = false;

    private int mTimeTurnOff = 0;

    private Handler mHandler;

    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.night_light);

        mAdView = new AdView(this, AdSize.BANNER, "a151aff80ce023b");
        LinearLayout layout = (LinearLayout)findViewById(R.id.nightlight);
        layout.addView(mAdView);
        mAdView.loadAd(new AdRequest());

        getCamera();
        startPreview();

        if (isSupportedFlashModes() == false) {
            Toast.makeText(this, R.string.not_support_flashlight, Toast.LENGTH_LONG).show();

            finish();
        }

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (mTimeTurnOff != 0) {
                    Camera.Parameters parameters = mCamera.getParameters();

                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    mCamera.setParameters(parameters);
                    mCamera.stopPreview();

                    mTimeTurnOff = 0;
                }
            }
        };

        mFifteenMinutes = (Button)findViewById(R.id.fifteen_minutes);
        mTwentyMinutes = (Button)findViewById(R.id.twenty_minutes);
        mThirtyMinutes = (Button)findViewById(R.id.thirty_minutes);
        mFortyFiveMinutes = (Button)findViewById(R.id.fortyfive_minutes);
        mSixtyMinutes = (Button)findViewById(R.id.sixty_minutes);
        mNinetyMinutes = (Button)findViewById(R.id.ninety_minutes);
        mTwoHours = (Button)findViewById(R.id.two_hours);
        mExit = (Button)findViewById(R.id.exit);

        mFifteenMinutes.setOnClickListener(this);
        mTwentyMinutes.setOnClickListener(this);
        mThirtyMinutes.setOnClickListener(this);
        mFortyFiveMinutes.setOnClickListener(this);
        mSixtyMinutes.setOnClickListener(this);
        mNinetyMinutes.setOnClickListener(this);
        mTwoHours.setOnClickListener(this);
        mExit.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mHandler.removeMessages(0);
        mHandler = null;

        releaseCamera();

        mAdView.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fifteen_minutes:
                onNightLight(TURN_OFF_AFTER_15MINUTES);
                break;
            case R.id.twenty_minutes:
                onNightLight(TURN_OFF_AFTER_20MINUTES);
                break;
            case R.id.thirty_minutes:
                onNightLight(TURN_OFF_AFTER_30MINUTES);
                break;
            case R.id.fortyfive_minutes:
                onNightLight(TURN_OFF_AFTER_45MINUTES);
                break;
            case R.id.sixty_minutes:
                onNightLight(TURN_OFF_AFTER_60MINUTES);
                break;
            case R.id.ninety_minutes:
                onNightLight(TURN_OFF_AFTER_90MINUTES);
                break;
            case R.id.two_hours:
                onNightLight(TURN_OFF_AFTER_120MINUTES);
                break;
            case R.id.exit:
                finish();
                break;
        }
    }

    private void getCamera() {
        if (mCamera == null) {
            mCamera = Camera.open();
        }

        if (mCamera != null) {
            mParameters = mCamera.getParameters();
        }
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    private void startPreview() {
        if ((mCameraPreviewing == false) && (mCamera != null)) {
            mCamera.startPreview();
            mCameraPreviewing = true;
        }
    }

    private void stopPreview() {
        if ((mCameraPreviewing == true) && (mCamera != null)) {
            mCamera.stopPreview();
            mCameraPreviewing = false;
        }
    }

    private boolean isSupportedFlashModes() {
        if (mParameters == null)
            return false;

        List<String> supportedFlashModes = mParameters.getSupportedFlashModes();
        boolean supported = true;

        if (supportedFlashModes != null) {
            for(String mode : supportedFlashModes) {
                if(mode.equals(Parameters.FLASH_MODE_TORCH)) {
                    supported = true;
                    break;                  
                } else {
                    supported = false;
                }
            }
        } else {
            supported = false;
        }

        return supported;
    }

    private void onNightLight(int time) {
        if (mTimeTurnOff != 0) { //if flashlight turns on
            if (mTimeTurnOff == time) {
                turnOffNightLight();
            } else {
                updateNightLight(time);
            }
        } else { //if flashlight turns off
            turnOnNightLight(time);
        }
    }

    private void updateNightLight(int time) {
        mHandler.removeMessages(0);

        mTimeTurnOff = time;
        mHandler.sendEmptyMessageDelayed(0, mTimeTurnOff);
    }

    private void turnOnNightLight(int time) {
        mParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(mParameters);

        mTimeTurnOff = time;

        mHandler.sendEmptyMessageDelayed(0, mTimeTurnOff);
    }

    private void turnOffNightLight() {
        mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(mParameters);

        mTimeTurnOff = 0;

        mHandler.removeMessages(0);
    }
}
